package com.ruslan.magaz.web;


import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ruslan.magaz.domain.Cart;
import com.ruslan.magaz.domain.Category;
import com.ruslan.magaz.domain.Customer;
import com.ruslan.magaz.domain.Product;
import com.ruslan.magaz.domain.Rating;
import com.ruslan.magaz.domain.User;
import com.ruslan.magaz.service.ItemService;
import com.ruslan.magaz.service.OrderService;
import com.ruslan.magaz.service.UserService;

@Controller
@RequestMapping("/admin/category")
public class AdminCategory {
	public static int PRODUCTS_PAGE_SIZE = 25;
	public static int REVIEWS_PAGE_SIZE = 5;

	@Autowired
	private UserService userService;
	
	@Autowired
	private ItemService itemService;
	
	@Autowired
	private OrderService orderService;
	
	
	
	@RequestMapping(value="")
    public String adminCategories(HttpServletRequest req, HttpServletResponse res, @RequestParam(required = false) Integer pageNum, Model model) throws ServletException, IOException{
			if (pageNum == null){
				pageNum=1;
			}
			int lastPageNum = itemService.categoriesPageCount(PRODUCTS_PAGE_SIZE);
	 		List<Category> categories = itemService.getAllCategories(pageNum, PRODUCTS_PAGE_SIZE);
	 		model.addAttribute("categories", categories);
	 		model.addAttribute("lastPageNum", lastPageNum);
	        return "adminTiles/adminCategories";
	 }
	
	@RequestMapping(value="/move", method = RequestMethod.POST)
    public String moveProductsFromCategory(HttpServletRequest req, HttpServletResponse res, @RequestParam String fromName, @RequestParam String toName, Model model) throws ServletException, IOException{
		int pageNum=1;
		Category from = itemService.getCategoryByName(fromName);
		Category to = itemService.getCategoryByName(toName);
		if (from == null || to == null){
			int lastPageNum = itemService.categoriesPageCount(PRODUCTS_PAGE_SIZE);
			List<Category> categories = itemService.getAllCategories(pageNum, PRODUCTS_PAGE_SIZE);
			model.addAttribute("categoryNotExists",true);
			model.addAttribute("categories", categories);
	 		model.addAttribute("lastPageNum", lastPageNum);
			return "adminTiles/adminCategories";
		}
 		itemService.moveProductsFromCategory(from.getCategoryId(), to.getCategoryId());
        return "redirect:/admin/category";
	 }
	
	@RequestMapping(value="/remove")
    public String removeCategory(HttpServletRequest req, HttpServletResponse res, @RequestParam Integer categoryId, RedirectAttributes redir) throws ServletException, IOException{
			if (itemService.countItemsInCategory(categoryId)>0){
				redir.addFlashAttribute("categoryNotEmpty",true);
				return "redirect:/admin/category"; 
			}
	 		itemService.removeCategory(categoryId);
	        return "redirect:/admin/category";
	 }
	
	@RequestMapping(value="/add", method= RequestMethod.GET)
    public String addCategory(HttpServletRequest req, HttpServletResponse res, Model model) throws ServletException, IOException{
			Category category = new Category();
			model.addAttribute("category", category);
			model.addAttribute("add", true);
			
	        return "adminTiles/adminAddCategory";
	 }
	
	@RequestMapping(value="/add", method= RequestMethod.POST)
    public String addCategory(HttpServletRequest req, HttpServletResponse res, Model model, @Valid @ModelAttribute("category") Category category, BindingResult bindingResult) throws ServletException, IOException{
		if(bindingResult.hasErrors()){
			 return "adminTiles/adminAddCategory";
		 }
		if (itemService.getCategoryByName(category.getName())!=null){
			model.addAttribute("categoryExists", true);
			return "adminTiles/adminAddCategory";
		}
		itemService.addCategory(category);
        return "redirect:/admin/category";
	 }
	
	@RequestMapping(value="/edit")
    public String editProduct(HttpServletRequest req, HttpServletResponse res, @RequestParam Integer categoryId, Model model) throws ServletException, IOException{
	 		Category category = itemService.getCategoryById(categoryId);
	 		model.addAttribute("category", category);
	 		model.addAttribute("add", false);
	        return "adminTiles/adminAddCategory";
	}
	
	@RequestMapping(value="/edit", method=RequestMethod.POST)
    public String editProductPost(HttpServletRequest req, HttpServletResponse res, Model model,	@Valid @ModelAttribute Category category, BindingResult bindingResult) throws ServletException, IOException{
		if(bindingResult.hasErrors()){
			 return "adminTiles/adminAddCategory";
		 }
		Category existsingCategory =  itemService.getCategoryByName(category.getName());
		if (existsingCategory.getCategoryId()!=category.getCategoryId()){
			model.addAttribute("categoryExists", true);
			return "adminTiles/adminAddCategory";
		}
		itemService.editCategory(category);
	    return "redirect:/admin/category";
	}
}
